package com.example.fourtestapp.utils;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.MotionEventCompat;
import androidx.slidingpanelayout.widget.SlidingPaneLayout;

public class mSlidingPaneLayout extends SlidingPaneLayout {

    private float mX;
    private float mY;
    private float mEdge;

    public mSlidingPaneLayout(@NonNull Context context) {
        this(context, null);
    }

    public mSlidingPaneLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public mSlidingPaneLayout(@NonNull Context context, @Nullable AttributeSet attrs,
                              int defStyle) {
        super(context, attrs, defStyle);
        ViewConfiguration config = ViewConfiguration.get(context);
        mEdge = config.getScaledEdgeSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        switch (MotionEventCompat.getActionMasked(ev)) {
            case MotionEvent.ACTION_DOWN: {
                mX = ev.getX();
                mY = ev.getY();
                break;
            }
            case MotionEvent.ACTION_HOVER_MOVE: {
                final float x = ev.getX();
                final float y = ev.getY();
                if (mX > mEdge && !isOpen() && canScroll(this,
                        false, Math.round(x - mX), Math.round(x), Math.round(y))) {
                    MotionEvent cancelEvent = MotionEvent.obtain(ev);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL);
                    return super.onInterceptTouchEvent(cancelEvent);
                }
                break;
            }
        }
        return super.onInterceptTouchEvent(ev);
    }
}
